/**
 * This class is the controller for the main view for the application. It is specified as
 * the "controller" of the Main view class.
 *
 * TODO - Replace this content of this view to suite the needs of your application.
 */
Ext.define('DragonApp.view.main.Controller', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.main',

    requires: [
        'DragonApp.util.Msg'
    ],

    /**
     * 登出
     *
     * @param  {object} button  登出按钮
     * @param  {[type]} e       事件
     * @param  {[type]} options 选项
     * @return {void}         返回信息
     */
    onLogout: function(button, e, options) {

        var me = this;
        Ext.data.JsonP.request({

            url: DragonApp.ux.config.Config.getUrl('/index/Account/doLogout/'),
            callbackKey: "callback",
            async: false,
            scope: me,
            success: 'onLogoutSuccess',
            failure: 'onLogoutFailure'
        });
    },

    /**
     * 登出成功
     *
     * @param  {object} result 返回信息
     * @return void
     */
    onLogoutSuccess: function(result) {
        switch (result.success) {
            case true:
                //登出成功
                sessionStorage.removeItem('DragonApp');
                sessionStorage.removeItem('DragonApp_user');
                sessionStorage.removeItem('DragonApp_role_name');

                if (result.success) {

                    this.getView().destroy();
                    window.location.reload();
                } else {
                    DragonApp.util.Msg.showErrorMsg(result.msg);
                }
                break;

            case false:

                DragonApp.util.Msg.showErrorMsg(result.msg);
                console.log('onLogoutSuccessFailure');

                //复位超时监控计时
                this.resetSessionMonitor();
                break;
        }
    },

    /**
     * 登出失败
     *
     * @param  {[type]} response [description]
     * @return {[type]}          [description]
     */
    onLogoutFailure: function(response) {

        DragonApp.util.Msg.showErrorMsg(response);

        //复位超时监控计时
        this.resetSessionMonitor();
        console.log('onLogoutFailure');
    },

    /**
     * 复位超时监控计时
     *
     * @return {void}
     */
    resetSessionMonitor: function() {

        DragonApp.ux.session.SessionMonitor.stop();
        DragonApp.ux.session.SessionMonitor.start();
    },

    /**
     * 修改密码
     *
     * @return void
     */
    onChangePass: function() {
        var win = Ext.create('Ext.window.Window', {
            title: '修改密码',
            resizable: false,
            collapsible: false,
            animCollapse: false,
            maximizable: false,
            closable: true,
            draggable: true,
            width: 350,
            height: 160,
            items: [{
                xtype: 'changepass',
                width: 350,
                bodyPadding: 15
            }]
        });
        win.show();
    },

    /**
     * 改密提交
     *
     * @return {[type]} [description]
     */
    onPassChangeSubmit: function() {
        Ext.data.JsonP.request({
            url: DragonApp.ux.config.Config.getUrl('/index/Account/changPass/'),
            callbackKey: "callback",
            async: false,
            scope: this,
            success: 'onChangPassSuccess',
            failure: 'onChangPassFailure'
        });
    },

    /**
     * 改密成功
     *
     * @return {[type]} [description]
     */
    onChangPassSuccess: function() {
        switch (result.success) {
            case true:
                DragonApp.util.Msg.showErrorMsg(result.msg);
                //关闭窗体
                var form = Ext.ComponentQuery.query('changepass')[0];
                var changePassWindow = form.up('window');
                changePassWindow.destroy();
                break;

            case false:
                DragonApp.util.Msg.showErrorMsg(result.msg);
                break;
        }
    },

    /**
     * 改密失败
     *
     * @param  {[type]} response [description]
     * @return {[type]}          [description]
     */
    onChangPassFailure: function(response) {
        DragonApp.util.Msg.showErrorMsg(response);
    },

    /**
     * 获取信息
     *
     * @return {[type]} [description]
     */
    getSysInfo: function() {
        Ext.data.JsonP.request({
            url: DragonApp.ux.config.Config.getUrl('/index/System/sysInfo/'),
            callbackKey: "callback",
            async: false,
            scope: this,
            success: 'onGetInfoSuccess',
            failure: 'onGetInfoFailure'
        });
    },

    /**
     * 获取信息成功
     *
     * @param  {[type]} result [description]
     * @return {[type]}        [description]
     */
    onGetInfoSuccess: function(result) {
        switch (result.success) {
            case true:
                this.showSysInfo(result.data);
                break;

            case false:
                DragonApp.util.Msg.showErrorMsg(result.msg);
                break;
        }
    },

    /**
     * 获取信息失败
     *
     * @param  {[type]} response [description]
     * @return {[type]}          [description]
     */
    onGetInfoFailure: function(response) {
        DragonApp.util.Msg.showErrorMsg(response);
    },

    /**
     * 显示系统信息
     *
     * @param    {[type]} data [description]
     * @return   {[type]}
     */
    showSysInfo: function(data) {

        var win = Ext.create('Ext.window.Window', {
            title: '系统信息',
            resizable: false,
            collapsible: false,
            animCollapse: false,
            maximizable: false,
            closable: true,
            draggable: true,
            width: 350,
            height: 180,
            items: [{
                xtype: 'appInfo',
                width: 350
            }]
        });
        var grid = Ext.ComponentQuery.query('appInfo')[0];
        grid.setSource({
            "Web 服务器": data.webserver,
            "PHP 版本": data.phpversion,
            "MySQL 版本": data.mysqlversion
            //"操作系统": data.osname,
            //"服务器 IP": data.serverip
        });
        win.show();
    },

    /**
     * 提交修改密码
     *
     * @param    {Ext.button.Button} button 确认按钮
     * @param    {[type]} e [description]
     * @param    {object} options [description]
     * @return   {[type]}
     */
    onConfirmChangeClick: function(button, e, options) {

        var basic = button.up("window").down("form");
        var form = basic.getForm();

        //用户名
        var _userField = Ext.ComponentQuery.query('hiddenfield[fieldLabel="用户名"]')[0];
        _userField.setValue(sessionStorage.getItem("DragonApp_user"));

        if (!form.isValid()) return;

        form.submit({
            waitMsg: "提交修改密码...",
            clientValidation: true,
            url: DragonApp.ux.config.Config.getUrl('/index/Account/doChangePass/'),

            success: function(form, action) {
                DragonApp.util.Msg.showToast(action.result.msg);
                button.up('window').close();
            },
            failure: function(form, action) {

                switch (action.failureType) {
                    case Ext.form.action.Action.CLIENT_INVALID:
                        Ext.Msg.alert('提示', 'Form fields may not be submitted with invalid values');
                        break;
                    case Ext.form.action.Action.CONNECT_FAILURE:
                        Ext.Msg.alert('提示', 'Ajax communication failed');
                        break;
                    case Ext.form.action.Action.SERVER_INVALID:
                        DragonApp.util.Msg.showToast(action.result.msg);
                }
            }
        });
    }
});